 
const cloud = require('wx-server-sdk');  
  
cloud.init();  
  
const db = cloud.database();  
  
// 云函数入口函数  
exports.main = async (event, context) => {  
  const { roomId, openid, avatarUrl, nickname } = event;  
  
  try {  
    
    const room = await db.collection('rooms').where({ roomId }).get();
  
    if (room.data.length === 0) {  
      return {  
        message: '房间不存在'  
      };  
    }  
    
    const roomData = room.data[0];  
    const { userList } = roomData;
  
  
    // 查找并更新指定的user  
    const userIndex = userList.findIndex(user => user.openid === openid);  
  
    if (userIndex === -1) {  
      return {  
        success: false,  
        message: 'User not found in the room',  
      };  
    }  
  
    // 更新user信息  
    userList[userIndex].avatarUrl = avatarUrl;  
    userList[userIndex].nickname = nickname;  
  
    // 更新数据库  
    await db.collection('rooms').doc(roomData._id).update({  
      data: {  
        userList: userList,  
      },  
    });  
  
    return {  
      success: true,  
      message: 'User information updated successfully',  
    };  
  } catch (error) {  
    return {  
      success: false,  
      message: error.message,  
    };  
  }  
};